package com.yq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.reggie.common.BaseContext;
import com.yq.reggie.mapper.OrdersMapper;
import com.yq.reggie.pojo.AddressBook;
import com.yq.reggie.pojo.OrderDetail;
import com.yq.reggie.pojo.Orders;
import com.yq.reggie.pojo.ShoppingCart;
import com.yq.reggie.service.AddressBookService;
import com.yq.reggie.service.OrderDetailService;
import com.yq.reggie.service.OrdersService;
import com.yq.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;
    /**
     * 支付
     * @param orders
     */
    @Override
    public void submit(Orders orders) {
        //添加数据到orders表
            //完善orders
            Long userId = BaseContext.getCurrentId();
            LocalDateTime orderTime = LocalDateTime.now();
            LocalDateTime checkoutTime = LocalDateTime.now();
            Long orderId = IdWorker.getId(); //订单id
        BigDecimal amount=new BigDecimal("0.00");
                LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ShoppingCart::getUserId,userId);
                List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
                for (ShoppingCart shoppingCart : shoppingCarts) {
                    BigDecimal number = BigDecimal.valueOf(shoppingCart.getNumber());
                    BigDecimal shopAmount = shoppingCart.getAmount();
                    amount = amount.add(shopAmount.multiply(number));
                }
                LambdaQueryWrapper<AddressBook> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(AddressBook::getUserId,userId);
                queryWrapper2.eq(AddressBook::getIsDefault,1);
                AddressBook addressBook = addressBookService.getOne(queryWrapper2);
            String phone = addressBook.getPhone();
            String address = addressBook.getDetail();
            String consignee = addressBook.getConsignee();
            orders.setUserId(userId);
            orders.setNumber(String.valueOf(orderId));
            orders.setOrderTime(orderTime);
            orders.setCheckoutTime(checkoutTime);
            orders.setAmount(amount);
            orders.setPhone(phone);
            orders.setStatus(2);
            orders.setAddress(address);
            orders.setConsignee(consignee);
         this.save(orders);
        //添加数据到order_detail表
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
            //完善orderDetail
            for (ShoppingCart shoppingCart : shoppingCarts) {
                OrderDetail item = new OrderDetail();
                item.setName(shoppingCart.getName());
                item.setImage(shoppingCart.getImage());
                item.setOrderId(orders.getId());
                if(shoppingCart.getSetmealId()==null){
                item.setDishId(shoppingCart.getDishId());
                item.setDishFlavor(shoppingCart.getDishFlavor());
                }else{
                    item.setSetmealId(shoppingCart.getSetmealId());
                }
                item.setNumber(shoppingCart.getNumber());
                item.setAmount(shoppingCart.getAmount());
                orderDetails.add(item);
            }
        orderDetailService.saveBatch(orderDetails);
        //清空当前用户的shopping_cart表
        LambdaQueryWrapper<ShoppingCart> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper3);
    }
}
